package com.stox.client.ui.javafx.widget;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.PopupControl;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;

import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.shared.i18n.MessageSource;

public class ConfigurationPopupControl extends PopupControl {

	private final BorderPane contentPane = new BorderPane();
	private final Button applyButton = UI.style(new Button(MessageSource.get("Apply")), CSS.undecorated, CSS.green);
	private final Button cancelButton = UI.style(new Button(MessageSource.get("Cancel")), CSS.undecorated, CSS.red);
	private final HBox buttonsHBox = new HBox(applyButton, cancelButton);
	private final EventHandler<ActionEvent> hideEventHandler = new EventHandler<ActionEvent>() {
		@Override
		public void handle(final ActionEvent event) {
			hide();
		}
	};

	public ConfigurationPopupControl(final Node content) {
		UI.style(contentPane, CSS.stoxPopup);
		setAutoHide(true);
		centerOnScreen();
		final Pane pane = new StackPane(content);
		UI.style(pane, CSS.stoxPopupCenter);
		contentPane.setCenter(pane);
		applyButton.addEventFilter(ActionEvent.ACTION, hideEventHandler);
		cancelButton.addEventFilter(ActionEvent.ACTION, hideEventHandler);
		buttonsHBox.setMaxWidth(Double.MAX_VALUE);
		UI.style(buttonsHBox, CSS.stoxPopupButtonsPanel);
		contentPane.setBottom(buttonsHBox);
		getScene().setRoot(contentPane);
	}

	public Button getApplyButton() {
		return applyButton;
	}

	public Button getCancelButton() {
		return cancelButton;
	}

	public HBox getButtonsHBox() {
		return buttonsHBox;
	}

	public BorderPane getContentPane() {
		return contentPane;
	}
}
